package com.power.sifa.net.formact;

import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

public class QueryFormDataUtil {


    /**
     * 获取主表数据
     * @param formData
     * @return
     */
    public static Map<String, Object> getMasterData(Map<String, Object> formData) {
        Map<String, Object> masterObj = (Map<String, Object>) formData.get("master");
        if(null != masterObj) {
            return (Map<String, Object>) masterObj.get("model");
        }
        return null;
    }

    /**
     * 获取继承主表数据
     * @param table
     * @param formData
     * @return
     */
    public static Map<String, Object> getExtendData(String table, Map<String, Object> formData) {
        List<Map<String, Object>> list = (List<Map<String, Object>>) formData.get("extend");
        if(!CollectionUtils.isEmpty(list)) {
            for (Map<String, Object> v : list) {
                if(table.equals(v.get("table"))) {
                    return (Map<String, Object>) v.get("model");
                }
            }
        }
        return null;
    }

    /**
     * 获取从表数据
     * @param table
     * @param formData
     * @return
     */
    public static List<Map<String, Object>> getSlaveData(String table, Map<String, Object> formData) {
        List<Map<String, Object>> list = (List<Map<String, Object>>) formData.get("slave");
        if(!CollectionUtils.isEmpty(list)) {
            for (Map<String, Object> v : list) {
                if(table.equals(v.get("table"))) {
                    return (List<Map<String, Object>> ) v.get("model");
                }
            }
        }
        return null;
    }
}
